package com.ciei.dpagm.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ciei.dpagm.common.enums.FormBusinessModuleEnum;
import com.ciei.dpagm.common.service.BaseService;
import com.ciei.dpagm.entity.ChipParameter;
import com.ciei.dpagm.entity.FormComponent;
import com.ciei.dpagm.entity.FormPropertyDefinition;
import com.ciei.dpagm.mapper.FormComponentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author wangyan
 * @date 2021/06/01
 */
@Service
public class FormComponentService extends BaseService<FormComponentMapper, FormComponent> {

    @Lazy
    @Autowired
    private FormPropertyDefinitionService formPropertyDefinitionService;

    @Autowired
    private FormPropertyService formPropertyService;

    /**
     * 批量编辑表单组件
     * @param layoutId
     * @param componentArray
     * @return 表单属性是否重复
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean editBatchFormLayout(Integer layoutId,JSONArray componentArray){
        List<FormComponent> oldComponentList = findByProperties(new String[]{"layoutId"},new Integer[]{layoutId});
        List<FormComponent> newComponentList = new ArrayList<>();
        //判断组件属性是否重复
        Boolean isRepeatOfComponent = false;
        List<Integer> definitionIds = new ArrayList<>();
        for (int i = 0; i < componentArray.size(); i++) {
            JSONObject componentData = componentArray.getJSONObject(i);
            Integer propertyDefinitionId = componentData.getInteger("propertyDefinitionId");
            if (definitionIds.contains(propertyDefinitionId)) {
                isRepeatOfComponent = true;
                break;
            }
            definitionIds.add(propertyDefinitionId);

            FormComponent formComponent = new FormComponent();
            formComponent.setPropertyDefinitionId(propertyDefinitionId);
            formComponent.setLayoutId(layoutId);
            newComponentList.add(formComponent);
        }
        if (isRepeatOfComponent){
            return true;
        }
        //删除旧的数据
        if (!oldComponentList.isEmpty()){
            deleteBatchByIds(oldComponentList.stream().map(FormComponent::getComponentId).collect(Collectors.toList()));
        }
        //保存新的数据
        if (!newComponentList.isEmpty()){
            saveBatch(newComponentList);
        }
        return false;
    }

    /**
     * 覆盖表单组件
     * @param layoutId
     * @param classificationId
     * @param chipParameterList
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public List<Integer> overwriteByParameters(Integer layoutId,Integer classificationId,List<ChipParameter> chipParameterList){
        List<FormPropertyDefinition> definitionList = formPropertyDefinitionService.findByProperty(FormPropertyDefinition::getBusinessModule,FormBusinessModuleEnum.SLAVE_CHIP_PARAMETER.getModuleId());
        List<FormComponent> componentList = findByProperty(FormComponent::getLayoutId,layoutId);
        List<FormComponent> newComponentList = new ArrayList<>();
        for (ChipParameter chipParameter : chipParameterList){
            FormPropertyDefinition definition = definitionList.stream().filter(e ->e.getPropertyKey().equals(chipParameter.getSymbol())).findFirst().orElse(null);
            if (definition != null){
                FormComponent formComponent = new FormComponent();
                formComponent.setPropertyDefinitionId(definition.getPropertyDefinitionId());
                formComponent.setLayoutId(layoutId);
                newComponentList.add(formComponent);
            }
        }
        List<Integer> newDefinitionIds = newComponentList.stream().map(FormComponent::getPropertyDefinitionId).collect(Collectors.toList());
        List<FormComponent> deleteComponentList = componentList.stream().filter(e -> !newDefinitionIds.contains(e.getPropertyDefinitionId())).collect(Collectors.toList());
        if (!deleteComponentList.isEmpty()){
            removeByIds(deleteComponentList.stream().map(FormComponent::getComponentId).collect(Collectors.toList()));
            formPropertyService.deleteBatchByDefinitionIds(deleteComponentList.stream().map(FormComponent::getPropertyDefinitionId).collect(Collectors.toList()), FormBusinessModuleEnum.SLAVE_CHIP_PARAMETER.getModuleId(),classificationId);
        }
        if (!newComponentList.isEmpty()){
            insertBatch(newComponentList);
        }
        return newComponentList.stream().map(FormComponent::getComponentId).collect(Collectors.toList());
    }


}
